package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.VoiceCount;
import com.tanhua.model.mongo.VoiceUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

/**
 * 桃花传音功能服务实现类
 */
@DubboService
public class VoiceFunctionApiImpl implements VoiceFunctionApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 利用id查询限定次数表
     * @return
     */
    @Override
    public VoiceCount findByUserId(Integer userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        VoiceCount voiceCount = mongoTemplate.findOne(query, VoiceCount.class);
        return voiceCount;
    }


    /**
     * 更新活跃限定次数表的次数
     *
     * @param voiceCount
     */
    @Override
    public void updateAllVoiceCount(VoiceCount voiceCount) {
        Long currentTimeMillis = System.currentTimeMillis()-259200;//当前时间
        //减少数据操作，更新三天内有使用该功能的
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").gte(0));

        Update update = new Update();
        //每日凌晨更新次数
        update.set("receiveCount", voiceCount.getReceiveCount());//接收次数
        update.set("sendCount", voiceCount.getSendCount());//发送次数

        mongoTemplate.updateMulti(query, update, VoiceCount.class);
    }


    /**
     * 新使用的用户创建次数表
     */
    @Override
    public void saveVoiceCount(VoiceCount voiceCount) {
        voiceCount.setReceiveCount(3);//接收次数
        voiceCount.setSendCount(3);//发送次数
        voiceCount.setId(ObjectId.get());//主键id
        voiceCount.setDate(System.currentTimeMillis());//当前创建时间
        mongoTemplate.insert(voiceCount);
    }


    /**
     * 接收次数更新
     */
    @Override
    public void updateReceiveCount(VoiceCount voiceCount) {
        Query updateQuery = new Query(Criteria.where("userId").is(voiceCount.getUserId()));
        Update update = new Update();
        update.inc("receiveCount", -1);
        update.set("date", System.currentTimeMillis());
        mongoTemplate.updateFirst(updateQuery, update, VoiceCount.class);
    }


    /**
     * 发送次数更新
     */
    @Override
    public void updateSendCount(VoiceCount voiceCount) {
        Query updateQuery = new Query(Criteria.where("userId").is(voiceCount.getUserId()));
        Update update = new Update();
        update.inc("sendCount", -1);
        update.set("date", System.currentTimeMillis());
        mongoTemplate.updateFirst(updateQuery, update, VoiceCount.class);
    }


    /**
     * 查询已接收的音频的主键id
     *
     * @return
     */
    @Override
    public List<ObjectId> findVoiceUseId(Integer userId) {
        Query query = new Query(Criteria.where("userId").is(userId));

        List<VoiceUser> voiceUsers = mongoTemplate.find(query, VoiceUser.class);

        ArrayList<ObjectId> objectIdList = new ArrayList<>();
        if (voiceUsers == null) {
            return new ArrayList<ObjectId>();
        }
        //循环封装数据
        for (VoiceUser voiceUser : voiceUsers) {
            if (voiceUser != null || voiceUser.getVoiceId() != null) {
                objectIdList.add(voiceUser.getVoiceId());
            }
        }
        return objectIdList;
    }


    /**
     * 保存接收过的语音记录
     *
     * @param
     */
    @Override
    public void saveVoiceUser(Integer userId,ObjectId voiceId) {
        VoiceUser voiceUser = new VoiceUser();
        voiceUser.setId(ObjectId.get());
        voiceUser.setUserId(userId);
        voiceUser.setVoiceId(voiceId);
        mongoTemplate.insert(voiceUser);
    }
}